package com.listeningframework.boot.autoconfigure.security.overrides;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.listeningframework.boot.commons.http.HttpWebUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AbstractAuthenticationTargetUrlRequestHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

/**
 * overrides spring security logout success handler
 * @author listening
 * @version $Id: SimpleUrlLogoutSuccessHandler.java, v 0.1 2016年1月9日 下午9:18:47 listening Exp $
 */
public class AjaxLogoutSuccessHandler extends AbstractAuthenticationTargetUrlRequestHandler
                                           implements LogoutSuccessHandler {

    /**
     * @see LogoutSuccessHandler#onLogoutSuccess(HttpServletRequest,
     *      HttpServletResponse, Authentication)
     */
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
                                Authentication authentication) throws IOException, ServletException {
        if (!HttpWebUtils.isAjaxRequest(request)) {
            super.handle(request, response, authentication);
            return;
        }
        // HttpCorsUtils.allowCorsOnce(request, response);
        HttpWebUtils.writeJson(response, true, "success");
    }

}